package com.diyunapp.xilinapp.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.OrderGoodsOutBean;
import com.diyunapp.xilinapp.myapi.OrderMoneyChageInterface;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.diyunapp.xilinapp.utils.ScrollListViewDisplay;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/6/27.
 */
public class OrderOuterAdapter extends BaseAdapter implements View.OnClickListener {

    public List<OrderGoodsOutBean> mDatas;
    public Context myContext;
    public OrderMoneyChageInterface myInterface;

    public OrderOuterAdapter(List<OrderGoodsOutBean> mDatas, Context myContext, OrderMoneyChageInterface myInterface) {
        this.mDatas = mDatas;
        this.myContext = myContext;
        this.myInterface = myInterface;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("zhangxiaoxidebug", "position = " + position);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(myContext).inflate(R.layout.order_out_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ViewHolder myHolder = holder;
        final OrderGoodsOutBean currentdata = mDatas.get(position);
        final float originTotal = currentdata.getGoods_total_money();

        myHolder.orderStorenameTv.setText(currentdata.getStoreName());
        OrderInnerAdapter innerAdapter = new OrderInnerAdapter(currentdata.getGoodsList(), myContext,0,"");
        myHolder.orderGoodslistLv.setAdapter(innerAdapter);
        innerAdapter.notifyDataSetChanged();
        ScrollListViewDisplay.setListViewHeightBasedOnChildren(myHolder.orderGoodslistLv);
        if (currentdata.getFreight().equals("1")) {
            myHolder.orderYunfeeTv.setText("运费：￥" + currentdata.getGoods_total_freight());
        } else {
            myHolder.orderYunfeeTv.setText("运费：免运费");
        }
        myHolder.orderGoodsTv.setText("商品总价：￥" + currentdata.getStore_goods_total());

        //是否使用满即送的判断已在前期数据解析时处理了
        if(currentdata.getMansongInfo().isIfCanUse())
        {
            myHolder.orderMansongTv.setVisibility(View.VISIBLE);
            myHolder.orderMansongTv.setText(currentdata.getMansongInfo().getDesc()+": -"+currentdata.getMansongInfo().getMansong_discount());
        }
        else
        {
            myHolder.orderMansongTv.setVisibility(View.GONE);
        }

        myHolder.orderStoretotalMoneyTv.setText("￥" + currentdata.getGoods_total_money());
        currentdata.setSaySomething(myHolder.saytoSellerEt.getText().toString());
        Log.i("zhangxiaoxidebug","shuru-------"+myHolder.saytoSellerEt.getText().toString());
        myHolder.saytoSellerEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                currentdata.setSaySomething(s.toString());
                Log.i("zhangxaoxidebug","ceshi"+currentdata.getSaySomething());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        if (currentdata.getYhqList().size() > 0) {
            //设置PopupWindow  //选择优惠券列表显示相关功能
            View yhqroot = LayoutInflater.from(myContext).inflate(R.layout.poplistlayout, null);
            ListView yhqListview = (ListView) yhqroot.findViewById(R.id.pop_list_view);
            YHQListAdapter myAdapter = new YHQListAdapter(myContext, currentdata.getYhqList());
            yhqListview.setAdapter(myAdapter);

            final PopupWindow yhqPopuWindow = new PopupWindow(yhqroot, 480, LinearLayout.LayoutParams.WRAP_CONTENT);

            yhqListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    // TODO Auto-generated method stub
                    currentdata.setSelectetYHQ(position);
                    myHolder.selectedYhqTv.setText(currentdata.getYhqList().get(position).getDesc());
                    String disMoney = currentdata.getYhqList().get(position).getVoucher_price();
                    myHolder.yuqpriceMoneyTv.setText("抵用：￥" + disMoney);
                    float disMoneyfl = FormatConfirm.stringToFloat(disMoney);
                    float newtotalmoney = originTotal - disMoneyfl;
                    currentdata.setGoods_total_money(newtotalmoney);
                    myHolder.orderStoretotalMoneyTv.setText("￥" + currentdata.getGoods_total_money());
                    yhqPopuWindow.dismiss();
                    myInterface.changeTotalMoney(disMoneyfl);
                }
            });


            // 点击外部可关闭窗口
            yhqPopuWindow.setBackgroundDrawable(FormatConfirm.getDrawable(myContext));
            yhqPopuWindow.setOutsideTouchable(true);
            yhqPopuWindow.setFocusable(true);

            myHolder.selectedYhqTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    yhqPopuWindow.showAsDropDown(myHolder.dropdownYhqparentView);
                }
            });

            myHolder.userYuqLl.setVisibility(View.VISIBLE);
        } else {
            myHolder.userYuqLl.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public void onClick(View v) {

    }


    static class ViewHolder {
        @Bind(R.id.order_storename_tv)
        TextView orderStorenameTv;
        @Bind(R.id.order_goodslist_lv)
        ListView orderGoodslistLv;
        @Bind(R.id.sayto_seller_et)
        EditText saytoSellerEt;
        @Bind(R.id.order_yunfee_tv)
        TextView orderYunfeeTv;
        @Bind(R.id.order_goods_tv)
        TextView orderGoodsTv;
        @Bind(R.id.order_mansong_tv)
        TextView orderMansongTv;
        @Bind(R.id.order_storetotal_money_tv)
        TextView orderStoretotalMoneyTv;
        @Bind(R.id.selected_yhq_tv)
        TextView selectedYhqTv;
        @Bind(R.id.dropdown_yhqparent_view)
        View dropdownYhqparentView;
        @Bind(R.id.yuqprice_money_tv)
        TextView yuqpriceMoneyTv;
        @Bind(R.id.user_yuq_ll)
        LinearLayout userYuqLl;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
